package com.onedec.designer.controller;


import com.onedec.designer.entity.OnedecFloorPlan;
import com.onedec.designer.entity.OnedecProject;
import com.onedec.designer.service.FileService;
import com.onedec.designer.service.OnedecFloorPlanService;
import com.onedec.designer.service.OnedecProjectService;
import com.onedec.designer.util.Page;
import com.onedec.designer.util.Result;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("designer")

public class OnedecDesignerController {
    @Autowired
    private OnedecFloorPlanService onedecFloorPlanService;
    @Autowired
    private OnedecProjectService onedecProjectService;
    //    @Autowired
//    private OnedecAccountingVouchersService onedecAccountingVouchersService;
    @Autowired
    FileService fileService;

    @Value("${baseUploadUrl}")
    private String url;

    @RequestMapping("query")
    public Result selectAll(OnedecFloorPlan onedecFloorPlan, Page page, int num) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("floorProjectId", onedecFloorPlan.getFloorProjectId());
        map.put("floorStatus", onedecFloorPlan.getFloorStatus());
        map.put("pageIndex", page.getPageIndex());
        map.put("num", num);

        long dataCount = onedecFloorPlanService.accountnum();

        page.setTotalCount(dataCount);
        page.setTotalPage((dataCount - 1) / num + 1);

        List<Map<String, Object>> maps = onedecFloorPlanService.selectByMap(map);

        return new Result("200", "success", page, maps);
    }


    @RequestMapping("deleteAll")
    public Result getAway(@RequestParam(value = "nums[]") Integer[] nums) throws Exception {
        for (Integer i : nums) {
            onedecFloorPlanService.deleteById(i);
        }
        return new Result("200", "success", null, null);
    }

    //单独删除一个商品
    @RequestMapping("delete")
    public Result delete(int id) throws Exception {
        onedecFloorPlanService.deleteById(id);
        return new Result("200", "success", null, null);
    }

    //
    @RequestMapping("selectById")
    public Result selectById(int id) throws Exception {
        OnedecFloorPlan onedecFloorPlan = onedecFloorPlanService.selectById(id);
        List<OnedecFloorPlan> list = onedecFloorPlanService.selectByProjectId(onedecFloorPlan.getFloorProjectId());
        return new Result("200", "success", onedecFloorPlan, list);
    }
    //通过项目id查找
    @RequestMapping("selectByProjectId")
    public Result selectByProjectId(String projectNull) throws Exception {
        OnedecProject onedecProject = onedecProjectService.selectByProjectNull(projectNull);
        List<OnedecFloorPlan> list = onedecFloorPlanService.selectByProjectId(onedecProject.getProjectId());
        return new Result("200", "success", null, list);
    }


    //
    @RequestMapping("update")
    public Result update(@Validated OnedecFloorPlan onedecFloorPlan, BindingResult result) throws Exception {

        if (result.hasErrors()) {
            throw new RuntimeException("参数错误");
        }

        onedecFloorPlanService.update(onedecFloorPlan);
        return new Result("200", "success", null, null);
    }

    @RequestMapping("updateAll")
    @Transactional
    public Result updateAll(@RequestParam(value = "nums[]") Integer[] nums) throws Exception {
        for (Integer i : nums) {
            onedecFloorPlanService.updateById(i);
        }
        return new Result("200", "success", null, null);
    }

    @RequestMapping("updateById")
    public Result updateById(int id) throws Exception {

        onedecFloorPlanService.updateById(id);

        return new Result("200", "success", null, null);
    }

    @RequestMapping("rejectById")
    public Result rejectById(int id) throws Exception {

        onedecFloorPlanService.rejectById(id);

        return new Result("200", "success", null, null);
    }

    @RequestMapping("updatepic")
    @ApiOperation(value = "单个图片上传到七牛云")
    public Result updatepic(@RequestParam(value = "pic") MultipartFile upfile, int id) throws Exception {

        String fileName = upfile.getOriginalFilename();
        File file = new File(url + fileName);
        try {
            //将MulitpartFile文件转化为file文件格式
            upfile.transferTo(file);
            Map response = fileService.uploadFile(file);
            Object imageName = response.get("imgName");
            String picUrl = "http://qddyllf7e.bkt.clouddn.com/" + imageName;
            OnedecFloorPlan onedecFloorPlan = onedecFloorPlanService.selectById(id);
            onedecFloorPlan.setFloorPlan(picUrl);
            onedecFloorPlanService.update(onedecFloorPlan);
//               int newid=onedecAccountingVouchers.getAccountingEntryId();
            return new Result("200", "success", id, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("1000", "error", null, null);
        }

    }

    //
//
    @Transactional
    @RequestMapping("insert/{projectNo}")
    @ApiOperation(value = "多个图片上传到七牛云")
    public Result insert(@PathVariable("projectNo") String projectNo, @RequestParam(value = "img") MultipartFile[] upfile) throws Exception {

        //todo 查询项目id
        final OnedecFloorPlan onedecFloorPlan = new OnedecFloorPlan();
        final OnedecProject project = onedecProjectService.selectByProjectNull(projectNo);

        onedecFloorPlan.setFloorProjectId(project.getProjectId());
        onedecFloorPlan.setFloorStatus("2");
        for (MultipartFile files : upfile) {
            String fileName = files.getOriginalFilename();
            File file = new File(url + fileName);
            try {
                //将MulitpartFile文件转化为file文件格式
                files.transferTo(file);
                Map response = fileService.uploadFile(file);
                Object imageName = response.get("imgName");
                String picUrl = "http://qddyllf7e.bkt.clouddn.com/" + imageName;


                onedecFloorPlan.setFloorPlan(picUrl);
                onedecFloorPlanService.insertOne(onedecFloorPlan);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return new Result("200", "success", null, null);
    }
}
